package com.hzg.ssm.query;

import com.alibaba.druid.sql.PagerUtils;
import lombok.Getter;
import lombok.Setter;

import java.util.Collections;
import java.util.List;

/**
 * @ Description: TODO
 * @ Author: 卓哥
 * @ Date: 2020/7/5 19:28
 * @ Version: 1.0
 */
@Setter
@Getter
public class PageResult<T> {
    //用户传送过来的两个数据
    private int currentPage;
    private int pageSize;

    //数据库查询出来的
    private int totalCount;
    private List<T> data;

    //计算出来的三个值
    private int prevPage;
    private int nextPage;
    private int totalPage;

    /*当数据为0 的时候*/
    public static PageResult empty(int pageSize) {
        return new PageResult(1, pageSize, 0, Collections.emptyList());
    }

    public PageResult(int currentPage, int pageSize, int totalCount, List<T> data) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.data = data;
        //计算三个页面
        this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        //计算上一页
        this.prevPage = currentPage - 1 <= 1 ? 1 : currentPage - 1;
        //计算下一页
        this.nextPage = currentPage + 1 >= totalPage ? totalPage : currentPage + 1;
    }
}
